package no39;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/4/24 14:32
 * 数组中出现次数超过一半的数字
 * https://leetcode.cn/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/
 */
public class Solution {

    // 众数思想
    public int majorityElement(int[] nums) {
        int res = nums[0];
        int resCount = 1;
        for (int i = 1; i < nums.length; i++) {
            if (res == nums[i]) {
                resCount++;
            } else {
                resCount--;
            }
            if (resCount == 0) {
                res = nums[i];
                resCount = 1;
            }
        }
        return res;
    }
}
